package biznussoft.fieldservice360.lookup;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListView;
import android.widget.TextView;
import biznussoft.fieldservice360.R;
import biznussoft.fieldservice360.fragments.InventoryFragment;
import biznussoft.fieldservice360.fragments.MaterialFragment;
import biznussoft.fieldservice360.model.SiteModel;
import biznussoft.fieldservice360.utils.Session;


public class SitesLookup implements OnItemClickListener {
	
	LookupAdapter adapter;
	Dialog lookupDialog;
	boolean isFiltered;
	private ArrayList<SiteModel> data;  
	private ArrayList<SiteModel> filterList;
	private ListView lvLookup;
	private EditText etLookup;
	private Context mContext;
	String strCategory;
	
	public void getItemLookUp(Context context,String category){               
		mContext = context;
		strCategory = category;
		lookupDialog = new Dialog(mContext);
		lookupDialog.setContentView(R.layout.accountlookup);
		lookupDialog.setTitle("FieldService360");
		lvLookup = (ListView) lookupDialog.findViewById(R.id.lvAccountlookuplistview);
		etLookup = (EditText) lookupDialog.findViewById(R.id.etFilteraccount);
		adapter = new LookupAdapter(InventoryFragment.siteList);
		try{
		if(InventoryFragment.itemIdlist.size()>0){
		lvLookup.setAdapter(adapter);}
		etLookup.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
           
            	adapter.getFilter().filter(s);
            	
            }
            public void beforeTextChanged(CharSequence s, int start, int count,
            		int after) {
            	
            }
            public void afterTextChanged(Editable s) {
            	
            }
            });              
		
		lookupDialog.show();
		
		lvLookup.setOnItemClickListener(this);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	class LookupAdapter extends BaseAdapter implements Filterable{

		private  ValueFilter valueFilter;
	
		ArrayList<SiteModel> mStringFilterList = new ArrayList<SiteModel>();
		public LookupAdapter(ArrayList<SiteModel> list) {
			data = list;
			mStringFilterList = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

	  class ViewHolder{
			public TextView name,age;
		}
		
		@Override
		public View getView(int position, View v, ViewGroup parent) {
			ViewHolder holder;
			if(v== null){
				holder = new ViewHolder();
				LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(android.R.layout.simple_list_item_1, null);
				holder.name = (TextView) v.findViewById(android.R.id.text1);
				v.setTag(holder);
			}else{
				holder = (ViewHolder) v.getTag();
			}
			
			SiteModel b = data.get(position);
			holder.name.setText(b.getSiteName());
			
			
			return v;
		}
		
		
		 public Filter getFilter() {
	        	if(valueFilter==null) {

	        		valueFilter=new ValueFilter();
	        	}
	        	
	        	return valueFilter;
	        }
	        
	        private class ValueFilter extends Filter
	        {

	        	@Override
	        	protected FilterResults performFiltering(CharSequence constraint)
	        	{
	        		FilterResults results=new FilterResults();

	        			if(constraint!=null && constraint.length()>0){

	        				filterList=new ArrayList<SiteModel>();

	        				for(int i=0;i<mStringFilterList.size();i++){

	        					String mLastName = mStringFilterList.get(i).getSiteName();
	        					if(mLastName.toLowerCase().contains(constraint.toString()))
	        					{
	        						filterList.add(mStringFilterList.get(i));

	        					}
	        				}
	        				results.count=filterList.size();
	        				results.values=filterList;
	        				isFiltered = true;
	        			}else{
	        				isFiltered = false;
	        				results.count=mStringFilterList.size();
	        				results.values=mStringFilterList;

	        			}

	        			return results;
	        	}

	        	@SuppressWarnings("unchecked")
	        	@Override
	        	protected void publishResults(CharSequence constraint,
	        			FilterResults results) 
	        	{
	        		data = (ArrayList<SiteModel>) results.values;

	        		notifyDataSetChanged();

	        	}

		
	        }
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.lvAccountlookuplistview:
			SiteModel bean  = data.get(position);
			InventoryFragment.etSite.setText(bean.getSiteName());
			Session.sSiteId = bean.siteId;
			lookupDialog.dismiss();   
			break;
			
		default:
			break;
		}
	
	
	}

}
